const config = require('config');

const getLocation = () => {
  var that = this;
  const currentCity = {};
  wx.getLocation({
    type: 'gcj02',
    isHighAccuracy: true,
    success: function(res) {
      
      let latitude = res.latitude
      let longitude = res.longitude
      wx.request({
        url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${config.key}`,
        success: res => {
          currentCity.city = res.data.result.ad_info.city;
          currentCity.cityCode = res.data.result.ad_info.city_code.substring(3);
          wx.setStorageSync("currentCity", currentCity);
          that.setData({
            currentCity: currentCity
          })
        }
      })
    },
    fail: res => {
      //判断是否获得了用户地理位置授权
      wx.getSetting({
        success: (res) => {
          if (!res.authSetting['scope.userLocation']) {
            openConfirm()
          }
        }
      })
    }
  })

}
const openConfirm = () => {
  wx.showModal({
    content: '检测到您没打开定位权限，是否去设置打开？',
    confirmText: "确认",
    cancelText: "取消",
    success: function(res) {
      //点击“确认”时打开设置页面
      if (res.confirm) {
        wx.openSetting({
          success: (res) => {
          }
        })
      } else {
      }
    }
  });
}
module.exports = {
  getLocation: getLocation,
  openConfirm: openConfirm
}